﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BarberShop.BLL;
using BarberShop.Data;


public partial class SotresAdd : BasePage
{
    public int Id
    {
        get
        {
            return string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BandMoviePlayinfo();
            BandAdPlayinfo();
            BandBrandinfo();
            BandProviceinfo();
            BandCityinfo();
            BandStoresInfo();

        }
    }
    #region 绑定品牌
    public void BandBrandinfo()
    {
        this.drp_Brand_Id.DataSource = BrandInfoBLL.GetAllBrandList();
        this.drp_Brand_Id.DataTextField = "BrandName";
        this.drp_Brand_Id.DataValueField = "ID";
        this.drp_Brand_Id.DataBind();
    }
    #endregion

    #region 绑定身份
    public void BandProviceinfo()
    {
        this.drp_procive_Id.DataSource = BarberCommonBLL.GetAllProvinceList();
        this.drp_procive_Id.DataTextField = "ProName";
        this.drp_procive_Id.DataValueField = "ProID";
        this.drp_procive_Id.DataBind();
    }
    #endregion

    #region 绑定城市
    public void BandCityinfo()
    {
        if (this.drp_procive_Id.SelectedValue != "")
        {
            int proid = int.Parse(this.drp_procive_Id.SelectedValue);
            this.drp_city_Id.DataSource = BarberCommonBLL.GetCityListByPID(proid);
            this.drp_city_Id.DataTextField = "CityName";
            this.drp_city_Id.DataValueField = "CityID";
            this.drp_city_Id.DataBind();
        }
    }
    #endregion

    #region 绑定门店信息
    public void BandStoresInfo()
    {
        if (Id != 0)
        {
            var info = StoresInfoBLL.GetStoresInfo(Id);
            txt_address.Text = info.Address;
            txt_CbeginDate.Text = info.ContactBeginDate.ToString();
            txt_CDeadLine.Text = info.ContactDeadline.Value.ToString();
            txt_storename.Text = info.StoreName;
            txt_telname.Text = info.TelName;
            txt_telphone.Text = info.TelPhone;
            drp_Brand_Id.SelectedValue = info.Brand_ID.ToString();
            drp_city_Id.SelectedValue = info.City_ID.ToString();
            drp_procive_Id.SelectedValue = info.Province_ID.ToString();
            drp_adImgPlay_Id.SelectedValue = info.AdImgPlay_ID.ToString();
            drp_moviePlay_Id.SelectedValue = info.MoviePlay_ID.ToString();
        }
    }
    #endregion

    #region 绑定影片播放列表
    public void BandMoviePlayinfo()
    {
        this.drp_moviePlay_Id.DataSource = MoviePalyListBLL.GetMoviePalyList("");
        this.drp_moviePlay_Id.DataTextField = "Name";
        this.drp_moviePlay_Id.DataValueField = "ID";
        this.drp_moviePlay_Id.DataBind();
        this.drp_moviePlay_Id.Items.Insert(0, new ListItem { Text = "", Value = "0" });
    }
    #endregion

    #region 绑定广告播放列表
    public void BandAdPlayinfo()
    {
        this.drp_adImgPlay_Id.DataSource = AdImgPalyListBLL.GetAdPlayListInfo("");
        this.drp_adImgPlay_Id.DataTextField = "Name";
        this.drp_adImgPlay_Id.DataValueField = "ID";
        this.drp_adImgPlay_Id.DataBind();
        this.drp_adImgPlay_Id.Items.Insert(0, new ListItem { Text = "", Value = "0" });
    }
    #endregion

    protected void drp_procive_Id_SelectedIndexChanged(object sender, EventArgs e)
    {
        BandCityinfo();
    }
    //添加门店
    protected void btn_add_Click(object sender, EventArgs e)
    {
        StoresInfo info = new StoresInfo();
        if (Id != 0)//update
        {
            info = StoresInfoBLL.GetStoresInfo(Id);
            info.ModifyDate = DateTime.Now;
        }
        else //add
        {
            info.CreateDate = DateTime.Now;
            info.ModifyDate = DateTime.Now;
            info.VerUpdateStatus = 0;
        }
        info.Address = txt_address.Text.Trim();
        info.Brand_ID = int.Parse(drp_Brand_Id.SelectedValue);
        info.City_ID = int.Parse(drp_city_Id.SelectedValue);
        info.Province_ID = int.Parse(drp_procive_Id.SelectedValue);
        info.MoviePlay_ID = int.Parse(drp_moviePlay_Id.SelectedValue);
        info.AdImgPlay_ID = int.Parse(drp_adImgPlay_Id.SelectedValue);
        info.ContactBeginDate = DateTime.Parse(txt_CbeginDate.Text);
        info.ContactDeadline = int.Parse(txt_CDeadLine.Text);
        info.StoreName = txt_storename.Text.Trim();
        info.TelName = txt_telname.Text.Trim();
       

        info.TelPhone = txt_telphone.Text.Trim();
        bool r = false;
        if (Id != 0)
        {
            r = StoresInfoBLL.UpdateSaveInfo(info);
        }
        else
        {
            r = StoresInfoBLL.AddStoresInfo(info);
        }
        if (r)
        {
            MsgBox("门店保存成功！");
        }
        else { MsgBox("保存失败！"); }
    }
}
